package com.huijiet.base.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huijiet.base.common.CommonParam;
import com.huijiet.base.utils.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


@Component
@Slf4j
public class EnvPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        String[] activeProfiles = environment.getActiveProfiles();

        //生产环境
        if(activeProfiles[0].equals("prod")){

            System.out.println("----------> start with prod <-----------");
            //jar的方式启动
            if(SystemUtil.runWithJar()){
                System.out.println("----------> start with jar <-----------");
                readMySql();
            }
        }
    }

    private void readMySql(){
        String fileName = SystemUtil.jarLocation() + "/conf/mysql.json";

        System.out.println("----------->"+fileName);
        String content = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(fileName);
            content = IOUtils.toString(fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(fileInputStream);
        }

        if(content!=null){
            JSONObject json = JSON.parseObject(content);
            String host = json.getString("host");
            int port = json.getIntValue("port");
            String dbName = json.getString("dbName");
            String username = json.getString("username");
            String password = json.getString("password");

            System.out.println("---------------------");
            System.out.println("mysql.host: " + host);
            System.out.println("mysql.port: " + port);
            System.out.println("mysql.dbName: " + dbName);
            System.out.println("mysql.username: " + username);
            System.out.println("mysql.password: " + password);

            StringBuilder builder = new StringBuilder();
            builder.append("jdbc:mysql://");
            builder.append(host);
            builder.append(":");
            builder.append(String.valueOf(port));
            builder.append("/");
            builder.append(dbName);
            builder.append("?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Hongkong");
            String url = builder.toString();

            System.out.println("mysql.url: " + url);
            System.out.println("---------------------");

            CommonParam.MYSQL_URL = url;
            CommonParam.MYSQL_USERNAME = username;
            CommonParam.MYSQL_PASSWORD = password;
            CommonParam.MYSQL_NEED_CHANGE = true;
        }else {
            System.out.println("----- not need change mysql param");
        }

    }
}
